package com.sright.bcard.enterprise.web.controller;

import com.sright.bcard.common.service.share.dto.LoginUser;
import com.sright.bcard.content.share.service.TagService;
import com.sright.bcard.content.share.service.UserTagService;
import com.sright.bcard.common.service.share.mvcresolver.EnableLoginAuth;
import com.sright.bcard.user.share.request.TagAddParam;
import com.sright.bcard.user.share.request.UserTagAddParam;
import com.sright.common.response.SrightResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @author peter
 * @description
 * @date 2020/6/4
 */
@RestController
@RequestMapping("/index")
public class TagController extends BaseController{

    @Autowired
    private TagService tagService;

    @Autowired
    private UserTagService userTagService;

    /**
     * 标签库-创建标签
     * @param param
     * @return
     */
    @PostMapping("/addtags.json")
    @EnableLoginAuth
    public SrightResponse addtags(@Validated @RequestBody TagAddParam param) {
        return tagService.addTag(param.getName(), buildLoginUser());
    }

    /**
     * 标签库-标签列表
     * @param name
     * @return
     */
    @GetMapping("/gettags.json")
    @EnableLoginAuth
    public SrightResponse gettags(@RequestParam(required = false) String name) {
        return tagService.listTag(name, buildLoginUser());
    }

    /**
     * 标签库-删除标签
     * @param id
     * @return
     */
    @DeleteMapping("/droptags.json")
    @EnableLoginAuth
    public SrightResponse droptags(@RequestParam(required = false) Long id) {
        return tagService.removeTag(id, buildLoginUser());
    }

    /**
     * 标签库-删除用户标签
     * @param param
     * @return
     */
    @PostMapping("/addUserTags.json")
    @EnableLoginAuth
    public SrightResponse addUserTags(@Validated @RequestBody UserTagAddParam param) {
        Boolean ret = userTagService.addUserTag(param.getId(), param.getTagIds(), buildLoginUser());
        if (ret) {
            return SrightResponse.defaultSuccess();
        }

        return SrightResponse.defaultBusinessError("添加失败");
    }

    private LoginUser buildLoginUser() {
        LoginUser loginUser = new LoginUser();
        loginUser.setUserId(userContext.getUserId());
        loginUser.setToken(userContext.getToken());
        loginUser.setEnterpriseId(userContext.getEnterpriseId());
        return loginUser;
    }
}
